package obj;

import java.util.Random;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import static java.lang.Math.abs;

/**
 * 刷新游戏，再给定的频率下定时刷新
 */
public class flush implements Runnable{
    //刷新每个实体
    private All obj;
    public flush(All a){
        this.obj=a;
    }
    @Override
    public void run(){
        Lock lock=new ReentrantLock();
        while(obj.Is_Start) {
            lock.lock();
            move_drops();
            move_flys();
            lock.unlock();
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    private void move_drops(){
        //更新用户发射的子弹
        for(int i=0;i<obj.drop_user.size();i++){
            if(obj.drop_user.get(i).move(0,obj.drop_user.get(i).getspeed())) {
                for (int j = 0; j <obj.flys.size();j++){
                    if(abs(obj.drop_user.get(i).Getx()-obj.flys.get(j).Getx())<=40&&abs(obj.drop_user.get(i).Gety()-obj.flys.get(j).Gety())<=20){
                        //如果击中修改生命
                        ((player)(obj.flys.get(j))).changeLife(-1);
                        if(((player)(obj.flys.get(j))).IsDie()){
                            //如果该飞行物品死亡
                            obj.DelPlayer(obj.flys.get(j));
                            obj.DeDrop(obj.drop_user.get(i),2);
                            i--;
                            obj.scores++;
                            break;
                        }
                    }
                }
            }
            else{
                //如果子弹越界了就删除
                obj.DeDrop(obj.drop_user.get(i),2);
                i--;
            }
        }
        //更新敌人发射的子单飞
        for(int p=0;p<obj.drop_enemy.size();p++){
            if(obj.drop_enemy.get(p).move(0,obj.drop_enemy.get(p).getspeed())) {
                //没有越界,看是否击中玩家
                //判断玩家是否死亡在另一个方法里面
                if (abs(obj.drop_enemy.get(p).Getx() - obj.user.Getx()) <= 40 && abs(obj.drop_enemy.get(p).Gety() - obj.user.Gety()) <= 20) {
                    ((player)(obj.user)).changeLife(-1);
                    obj.DeDrop(obj.drop_enemy.get(p),1);
                    p--;
                }
            }
            else{
                //越界了
                obj.DeDrop(obj.drop_enemy.get(p),1);
                p--;
            }
        }
    }

    private void move_flys(){
        for (int i = 0; i < obj.flys.size(); i++) {
            //更新飞行位置
            if(RandomMove(obj.flys.get(i))) {//看是否越界
                if (abs(obj.flys.get(i).Getx() - obj.user.Getx()) < 50) {
                    //判断是否在飞行物上面
                    //在就直接开火
                    if(System.currentTimeMillis()-((player)(obj.flys.get(i))).Last_shoot>=1500) {
                        ((player) (obj.flys.get(i))).Last_shoot = System.currentTimeMillis();
                        obj.drop_enemy.add(new drops(obj.flys.get(i).Getx(), obj.flys.get(i).Gety(), 10, 1));
                    }
                }
            }
            else{//越界l
                obj.flys.remove(obj.flys.get(i));
                i--;
            }
        }
    }

    private boolean RandomMove(objs aim){
        Random r=new Random();
//        return aim.move(r.nextBoolean()?0:r.nextBoolean()?-1*aim.getspeed():aim.getspeed(),r.nextBoolean()?0:r.nextBoolean()?-1*aim.getspeed():aim.getspeed());
        return aim.move(aim.Getx()>obj.user.Getx()?-1*aim.getspeed():aim.getspeed(),r.nextBoolean()?0:r.nextBoolean()?-1*aim.getspeed():aim.getspeed());
    }
}
